package co.com.jtune.view.main;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListModel;

import org.jdesktop.application.ResourceMap;

import co.com.jtune.controller.LibraryController;
import co.com.jtune.model.TrackModel;
import co.com.jtune.services.ServiceFactory;
import co.com.jtune.view.tableModel.TrackTableModel;

import com.jgoodies.binding.adapter.SingleListSelectionAdapter;
import com.jgoodies.binding.list.SelectionInList;
import com.jgoodies.binding.value.ValueHolder;
import com.jgoodies.common.collect.ArrayListModel;

public class MusicListPanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JTable musicList;
	private TrackTableModel trackTableModel;
	private SelectionInList<TrackModel> tracksInList;
	private ListModel<TrackModel> arrayListModel;
	private ValueHolder valueHolder;
	
	private ResourceMap resource;
	
	private LibraryController libraryController;
	
	public MusicListPanel(ResourceMap resource) {
		super();
		this.resource = resource;
		
		buildComponents();
		createSimpleGui();
		refreshTable();
		valueHolder.fireValueChange(null, null);
	}

	private void createSimpleGui()
	{
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		add(new JScrollPane(musicList));
	}
	
	@SuppressWarnings("unchecked")
	private void buildComponents()
	{
		String [] columnNames = new String[]{"MusicListPanel.table.name",
				"MusicListPanel.table.duration",
				"MusicListPanel.table.artist",
				"MusicListPanel.table.album",
				"MusicListPanel.table.gener"};
		
		valueHolder = new ValueHolder(null);
		
		arrayListModel = new ArrayListModel<TrackModel>();
		
		tracksInList = new SelectionInList<TrackModel>(arrayListModel, valueHolder);
		
		trackTableModel = new TrackTableModel(tracksInList, columnNames, resource);
		
		musicList = new JTable(trackTableModel);
		musicList.setSelectionModel(new SingleListSelectionAdapter(tracksInList.getSelectionIndexHolder()));
		musicList.setPreferredScrollableViewportSize(new Dimension(500, 70));
		musicList.setFillsViewportHeight(true);
		musicList.setAutoCreateRowSorter(true);
		musicList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent mouseEvent) {
				if (mouseEvent.getClickCount() == 2) {
					@SuppressWarnings("all")
					TrackModel track = (TrackModel) valueHolder.getValue();
					mouseClickedEvent(track);
				}
			}
		});
		
		
		
		libraryController = ServiceFactory.getInstance().getLibraryController();
	}
	
	public void refreshTable()
	{
		List<TrackModel> trackModelList =  libraryController.getLibrary();
		
		this.tracksInList.getList().clear();
		this.tracksInList.getList().addAll(trackModelList);
		trackTableModel.fireTableDataChanged();
	}
	
	private void mouseClickedEvent(TrackModel track) {
		// TODO Se debe reproducir el track seleccionado
		
	}
	
	public void addNewTrack(TrackModel track) {
		this.tracksInList.getList().add(track);
	}
	
	public int getTrackIndex(TrackModel track) {
		return this.tracksInList.getList().indexOf(track);
	}
	
	public void updateElement(TrackModel track, int index) {
		this.tracksInList.getList().remove(index);
		this.tracksInList.getList().add(index, track);
	}

	public TrackModel getSelectTrack() {
		// TODO Auto-generated method stub
		return (TrackModel)valueHolder.getValue();
	}
	
//	public class TrackTableModel extends AbstractTableModel {
//
//		/**
//		 * 
//		 */
//		private static final long serialVersionUID = 1L;
//		
//		private String[] columnNames;
//		private ResourceMap resource;
//		private List<Track> tracks;
//		
//		public TrackTableModel(ResourceMap resource)
//	    {
//	    	this.resource = resource;
//	    	
//	    	columnNames = new String[]{ "  ",
//	    								this.resource.getString("MusicListPanel.table.name"),
//	    								this.resource.getString("MusicListPanel.table.duration"),
//	    								this.resource.getString("MusicListPanel.table.artist"),
//	    								this.resource.getString("MusicListPanel.table.album"),
//	    								this.resource.getString("MusicListPanel.table.gener")};
//	    	
//	    	
//	    	tracks = new ArrayList<Track>();
//	    }
//		
//		public int getColumnCount() {
//			// TODO Auto-generated method stub
//			return columnNames.length;
//		}
//		
//		public String getColumnName(int col) {
//	        return columnNames[col];
//	    }
//
//		public int getRowCount() {
//			// TODO Auto-generated method stub
//			return tracks.size();
//		}
//
//		public Object getValueAt(int row, int col) {
//			
//			Track track = getTracks().get(row);
//			
//	        switch (col) {
//			case 0:
//				return "";
//			case 1:	
//				return track.getName();
//			case 2:
//				return new Time(track.getDuration());
//			case 3:
//				return track.getArtist();
//			case 4:
//				return track.getAlbum();
//			case 5:
//				return track.getGenre();
//			default:
//				return "";
//			}
//		}
//		
//		public Object getValueAt(int row) {
//			
//			Track track = getTracks().get(row);
//			return track;
//		}
//		
//		protected Object eval(Track model, int columIndex) {
//	    	String column = columnNames[columIndex];
//	    	String path[] = column.split("\\.");
//	    	if (path.length == 1) {
//	    		Object value = ReflectionClientUtils.getPropertyValue(model, column);
//	    		if (value instanceof Boolean) {
//	    			return resource.getString(value.toString());
//	    		} else {
//	    			return value;
//	    		}
//	    	} else {
//	    		Object object = model;
//	    		for (String property : path) {
//	    			if (object != null) {
//	    				object = ReflectionClientUtils.getPropertyValue(object, property);
//	    			} else {
//	    				break;
//	    			}
//	    		}
//	    		return object;
//	    	}
//	    }
//		
//		public List<Track> getTracks() {
//			return tracks;
//		}
//		
//		public void setTrack(List<Track> tracks)
//		{
//			this.tracks = tracks;
//		}
//	
//	}
	
}
